import datetime
import re
import sys

from base_class.inspect_logging import print_to_log
from config import *
from data_load_up import DataStep

if __name__ == '__main__':
    date_str = None
    if len(sys.argv) > 1:
        var = sys.argv[1]
        match1 = re.search(r"(\d{4}-\d{1,2}-\d{1,2})", var)
        match2 = re.search(r"(\d{4}-\d{1,2})", var)
        if match1 or match2:
            date_str = var[0:7]
            print("输入参数", date_str)
        else:
            print_to_log("请输入正确参数")

    if date_str == None:
        today = datetime.datetime.today()
        # 上个月
        if today.month == 1:
            month = 12
            year = today.year - 1
        else:
            month = today.month - 1
            year = today.year
        date_str = datetime.date(year, month, 1).strftime('%Y-%m')
    returnstep = DataStep(s3_path=return_s3_path+f'p_date={date_str}/', url=return_url, local_path=return_local_path, buc=return_bucket)
    Next = returnstep.download(date_str)
    if Next:
        returnstep.upload()
